package com.liveneo.adapter.base.support;

import java.util.Date;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class BaseDaoImpl<D extends BaseMapper<T>, T extends BaseEntity> implements BaseDao<D, T> {
    @Autowired
    protected D mapper;

    @Override
    public T selectByPrimaryKey(String id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delete(T entity) {
        return deleteByPrimaryKey(entity.getId());
    }

    @Override
    public int insert(T entity) {
        if (entity.getId() == null) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            entity.setId(uuid);
        }
        if (entity.getCreateTime() == null) {
            entity.setCreateTime(new Date());
        }
        return mapper.insert(entity);
    }

    @Override
    public int insertSelective(T record) {
        return mapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKey(T record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return mapper.updateByPrimaryKeySelective(record);
    }
}
